package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebAdviseDao;
import ysq.xyg.web.model.po.WebAdvise;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebAdviseVo;
import ysq.xyg.web.service.WebAdviseService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-建议service实现
 *
 * @author shengQiang yu
 * @date 2019-4-26 9:34:21
 */
@Service("webAdviseService")
public class WebAdviseServiceImpl extends BaseServiceImpl<WebAdvise> implements WebAdviseService {

    @Resource
    private WebAdviseDao webAdviseDao;

    private WebAdvise webAdvise;

    @Autowired
    public void setBaseDao(WebAdviseDao webAdviseDao) {
        this.baseDao = webAdviseDao;
    }


    /**
     * 分页查询
     *
     * @param webAdviseVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 9:34:21
     */
    public ReturnEntity page(WebAdviseVo webAdviseVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" status asc,update_time desc");
        List<WebAdvise> list = acceptList(webAdviseVo);
        PageInfo<WebAdvise> page=new PageInfo<WebAdvise>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAdviseVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 9:34:21
     */
    public ReturnEntity addOrUpdate(WebAdviseVo webAdviseVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAdviseVo.getId())){
            webAdvise=new WebAdvise(currentUser);
            isUpate=false;
        }else{
            webAdvise=selectByPrimaryKey(webAdviseVo.getId());
            if(ObjectUtil.isNull(webAdvise)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webAdvise.setUpdateTime(new Date());
            webAdvise.setUpdateUser(currentUser.getId());
        }
        webAdvise.setStatus(webAdviseVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webAdvise);
        }else{
            num=insert(webAdvise);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 9:34:21
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebAdvise webAdvise=selectByPrimaryKey(id);
        if(webAdvise!=null){
            webAdvise.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webAdvise.setUpdateTime(new Date());
            webAdvise.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webAdvise);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webAdviseVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-26 9:34:21
     */
    public List<WebAdvise> acceptList(WebAdviseVo webAdviseVo){
        Example example = new Example(WebAdvise.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAdviseVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAdviseVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webAdviseVo.getPhone())){
            criteria.andLike("phone","%"+webAdviseVo.getPhone()+"%");
        }
        if(StringUtil.isNotEmpty(webAdviseVo.getTitle())){
            criteria.andLike("title","%"+webAdviseVo.getTitle()+"%");
        }
        if(StringUtil.isNotEmpty(webAdviseVo.getStatus())){
            criteria.andEqualTo("status",webAdviseVo.getStatus());
        }
        example.setOrderByClause(" status asc,update_time desc");
        return webAdviseDao.selectByExample(example);
    }
}
